/**
 * swfupload general style settings
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: settings.js 17 2013-04-02 03:28:47Z zhouyonglong $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
var swfu_settings = {

    /**
     * Upload backend settings
     */
    upload_url : URLBASE + 'swfupload/general',
    file_post_name : 'swfupload',

    /**
     * File settings
     */
    file_types : '*.jpg;*.gif;*.png',
    file_types_description  : '所有图片文件',
    file_size_limit : '2 MB',

    /**
     * Upload backend settings
     */
    flash_url : URLBASE + 'scripts/swfupload/swfupload.swf',
    flash9_url : URLBASE + 'scripts/swfupload/swfupload_fp9.swf',

    /**
     * Button settings
     */
    button_image_url : URLBASE + 'scripts/swfupload/general/btn_120x30.png',
    button_width : 120,
    button_height : 30,
    button_text : '选择上传文件 ...',
    //button_text_style : 'font-size: 16pt; font-weight: bold;',
    button_text_top_padding : 5,
    button_text_left_padding : 15,
    button_action : SWFUpload.BUTTON_ACTION.SELECT_FILE, // 上传单个文件

    /**
     * 加载前预处理
     */
    swfupload_preload_handler : function(){
        if (!this.support.loading) {
            alert('您需要 Flash Player 9.028 以上版本');
            return false;
        }
    },

    /**
     * 加载失败
     */
    swfupload_load_failed_handler : function(){
        alert('加载上传程序时发生错误');
    },

    /**
     * 当文件成功被添加到队列中
     *
     * @param  string  file
     */
    file_queued_handler : function(file){
        var progress = new SWFU_Progress(file, this.settings.custom_settings.progress_target);
    },

    /**
     * 已经完成文件选择与上传队列
     *
     * @param  integer  numFilesSelected
     * @param  integer  numFilesQueued
     */
    file_dialog_complete_handler : function(numFilesSelected, numFilesQueued){
        this.startUpload();
    },

    /**
     * 文件上传队列错误
     *
     * @param  string   file
     * @param  integer  errorCode
     * @param  string   message
     */
    file_queue_error_handler : function(file, errorCode, message){
        if (errorCode == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
            alert('文件数量不能超过 ' + this.settings.file_queue_limit + ' 个');
            return false;
        }

        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                message = '超出限制大小 (' + this.settings.file_size_limit + ')';
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                message = '无法上传 0 字节文件';
                break;
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                message = '无效的上传文件类型';
                break;
        }

        var progress = new SWFU_Progress(file, this.settings.custom_settings.progress_target);
        progress.setError(message);
    },

    /**
     * 上传文件之前进行处理
     *
     * @param  string  file
     */
    upload_start_handler : function(file){
        this.setButtonDisabled(true);
        this.callFlash("SetButtonText", ['上传中…']);
        this.callFlash("SetButtonTextPadding", [4, 1]);
    },

    /**
     * 上传进度处理
     *
     * @param  string   file
     * @param  integer  bytesLoaded
     * @param  integer  bytesTotal
     */
    upload_progress_handler : function(file, bytesLoaded, bytesTotal) {
        var progress = new SWFU_Progress(file, this.settings.custom_settings.progress_target);
        progress.update(bytesLoaded);
    },

    /**
     * 上传错误处理
     *
     * @param  string   file
     * @param  integer  errorCode
     * @param  string  message
     */
    upload_error_handler : function(file, errorCode, message) {
        switch (errorCode) {
            case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
                message = 'HTTP 错误';
                break;
            case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
                message = '缺少上传网址';
                break;
            case SWFUpload.UPLOAD_ERROR.IO_ERROR:
                message = 'IO 错误';
                break;
            case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
                message = '安全校验错误';
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                message = '超出限制大小 (' + this.settings.file_size_limit + ')';
                break;
            case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
                message = '未找到指定文件';
                break;
            case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
                message = '文件校验失败';
                break;
            case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                message = '文件已取消';
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                message = '上传被中断';
                break;
        }
        var progress = new SWFU_Progress(file, this.settings.custom_settings.progress_target);
        progress.setError(message);
        this.setButtonDisabled(false);
        this.callFlash("SetButtonText", [this.settings.button_text]);
        this.callFlash("SetButtonTextPadding", [this.settings.button_text_left_padding, this.settings.button_text_top_padding]);
    },

    /**
     * 上传成功
     *
     * @param  string  file
     * @param  string  serverData
     */
    upload_success_handler : function(file, serverData) {
        if (typeof this.settings.custom_settings.session_url == 'string') {
            $.post(this.settings.custom_settings.session_url, { session_id : this.settings.post_params.session_id });
        }
        var data = $.parseJSON(serverData);
        var progress = new SWFU_Progress(file, this.settings.custom_settings.progress_target);
        if (data != null && data.success) {
            if (typeof this.settings.custom_settings.success_callback == 'function') {
                this.settings.custom_settings.success_callback(data, this.settings);
            }
            progress.complete();
        } else if (data.message) {
            progress.setError(data.message);
        } else {
            progress.setError('未知错误');
        }
        this.setButtonDisabled(false);
        this.callFlash("SetButtonText", [this.settings.button_text]);
        this.callFlash("SetButtonTextPadding", [this.settings.button_text_left_padding, this.settings.button_text_top_padding]);
    },

    /**
     * 完成上传
     */
    upload_complete_handler : null

};

/**
 * swfupload for jquery
 *
 * <example>
 * $('input[type=text]').swfupload({upload_url : 'swfupload.php', ...});
 * </example>
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: settings.js 17 2013-04-02 03:28:47Z zhouyonglong $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
$.fn.swfupload = function(options){
    if ($(this).is('input')) { // input file 控件
        // 创建包裹层
        var wrap = $('<div></div>');

        // 创建上传控件
        var placeholder_id = 'placeholder_' + Math.round(Math.random() * 1000000);
        wrap.append('<div id="' + placeholder_id + '"></div>');

        // 合并设置
        var settings = $.extend(swfu_settings, {button_placeholder_id : placeholder_id, custom_settings : { progress_target : $('<div></div>') }}, options);

        // 替换显示
        $(this).hide().after(wrap);

        // 实例化
        this.swfupload = new SWFUpload(settings);
    }

    return this;
};